//
//  WorkManController.m
//  operativeSide-2.0
//
//  Created by 王春程 on 2016/12/15.
//  Copyright © 2016年 王春程. All rights reserved.
//

#import "WorkManController.h"
#import "CCDWorkManTableView.h"
#import "CCDHaveRecStaffTableView.h"
#import "CCDHaveRecTableView.h"
#define BottomViewFrame self.bottomView.frame.size.height

@interface WorkManController ()<UIScrollViewDelegate,CCDWorkManDelegate>
@property (strong, nonatomic)                   UIView *backView;


@property (strong, nonatomic)                   UIButton *waitBtn;
@property (strong, nonatomic)                   UIButton *staffBtn;
@property (strong, nonatomic)                   UIButton *haveRecBtn;

@property (strong, nonatomic)                   UIScrollView *scrollView;
@property (strong, nonatomic)                   UIView *changeView;
@property (strong, nonatomic)                   UILabel *lineLabel;
@property (strong, nonatomic)                   UIView *bottomView;

///三个界面
@property (strong, nonatomic)                   CCDWorkManTableView *workManTableView;
@property (strong, nonatomic)                   CCDHaveRecStaffTableView *staffTableView;
@property (strong, nonatomic)                   CCDHaveRecTableView *haveRecTableView;

@property (strong, nonatomic)                   WaitJPieces *piece;

@property (assign, nonatomic)                   NSInteger allIndex;
@end

@implementation WorkManController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSubViews];
    // Do any additional setup after loading the view from its nib.
}

- (void)initSubViews {
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 64)];
    _backView.backgroundColor = RGB(253, 213, 47);
    [self.view addSubview:self.backView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    label.text = @"业务员";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = RGB(51, 51, 51);
    label.font = [UIFont systemFontOfSize:18.0f];
    [self.backView addSubview:label];
    
    _changeView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, 30)];
    _changeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.changeView];
    
    //待收件
    _waitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _waitBtn.selected = YES;
    [_waitBtn setTitle:@"待收件" forState:UIControlStateNormal];
    _waitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_waitBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [_waitBtn setTitleColor:RGB(253, 213, 47) forState:UIControlStateSelected];
    [_waitBtn addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    _waitBtn.frame= CGRectMake(0, 0, VIEW_WIDTH/3, 27);
    _waitBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.changeView addSubview:self.waitBtn];
    
    //已接单
    _staffBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_staffBtn setTitle:@"已接单" forState:UIControlStateNormal];
    _staffBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_staffBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [_staffBtn setTitleColor:RGB(253, 213, 47) forState:UIControlStateSelected];
    [_staffBtn addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    _staffBtn.frame= CGRectMake(VIEW_WIDTH/3, 0, VIEW_WIDTH/3, 27);
    _staffBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.changeView addSubview:self.staffBtn];
    
    //已收件
    _haveRecBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_haveRecBtn setTitle:@"已收件" forState:UIControlStateNormal];
    _haveRecBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_haveRecBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [_haveRecBtn setTitleColor:RGB(253, 213, 47) forState:UIControlStateSelected];
    [_haveRecBtn addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    _haveRecBtn.frame = CGRectMake(VIEW_WIDTH * 2/3, 0, VIEW_WIDTH/3, 27);
    _haveRecBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.changeView addSubview:self.haveRecBtn];
    
    
    _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,27, VIEW_WIDTH/3, 3)];
    _lineLabel.backgroundColor = RGB(253, 213, 47);
    [self.changeView addSubview:self.lineLabel];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 94, VIEW_WIDTH, VIEW_HEIGHT - 64 -30)];
    [self.view addSubview:self.bottomView];
    
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,VIEW_WIDTH, BottomViewFrame)];
    [_scrollView setBounces:NO];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setShowsVerticalScrollIndicator:NO];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setContentOffset:CGPointZero];
    _scrollView.delegate = self;
    [_scrollView setContentSize:CGSizeMake(VIEW_WIDTH * 3, self.bottomView.frame.size.height)];
    [self.bottomView addSubview:self.scrollView];
    
    //加载派送员界面
    _workManTableView = [[CCDWorkManTableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT - 94) style:UITableViewStylePlain];
    _workManTableView.workDelegate = self;
    [self.scrollView addSubview:self.workManTableView];
    
    _staffTableView = [[CCDHaveRecStaffTableView alloc] initWithFrame:CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, VIEW_HEIGHT - 94) style:UITableViewStylePlain];
    [self.scrollView addSubview:self.staffTableView];
    
    _haveRecTableView = [[CCDHaveRecTableView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * 2, 0, VIEW_WIDTH, VIEW_HEIGHT - 94) style:UITableViewStylePlain];
    [self.scrollView addSubview:self.haveRecTableView];
    
}

//头部按键跳转逻辑设定
- (void)pressAction:(UIButton *)sender {
    //点击按键发出通知，确认当前界面的跳转以及刷新
    if(sender == self.waitBtn) {
        //点击时进行刷新
        [self.workManTableView.mj_header beginRefreshing];
        self.waitBtn.selected = YES;
        self.staffBtn.selected = NO;
        self.haveRecBtn.selected = NO;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lineLabel.frame = CGRectMake(0, 27, VIEW_WIDTH/3, 3);
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }];
    } else if(sender == self.staffBtn){
        [self.staffTableView.mj_header beginRefreshing];
        self.staffBtn.selected = YES;
        self.waitBtn.selected = NO;
        self.haveRecBtn.selected = NO;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lineLabel.frame = CGRectMake(VIEW_WIDTH/3, 27, VIEW_WIDTH/3, 3);
            self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
        }];
    } else {
        [self.haveRecTableView.mj_header beginRefreshing];
        self.staffBtn.selected = NO;
        self.waitBtn.selected = NO;
        self.haveRecBtn.selected = YES;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lineLabel.frame = CGRectMake(VIEW_WIDTH * 2/3, 27, VIEW_WIDTH/3, 3);
            self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH * 2, 0);
        }];
    }
}


#pragma mark -_- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.lineLabel setFrame:CGRectMake(scrollView.contentOffset.x/3, 27, VIEW_WIDTH/3, 3)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint offSet = scrollView.contentOffset;
    if(offSet.x == 0) {
        [self pressAction:self.waitBtn];
    } else if(offSet.x == VIEW_WIDTH){
        [self pressAction:self.staffBtn];
    } else {
        [self pressAction:self.haveRecBtn];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
